package com.atguigu.srb.base.algorithm;

import org.springframework.util.StopWatch;

import java.util.*;

/**
 * @ClassName Solution4
 * @Describe
 * @Author Wangchengshan
 * @Date 2023/5/15 14:14
 * @Version 1.0
 **/
public class Solution4 {

    public static boolean lemonadeChange(int[] bills) {
        int five = 0, ten = 0;
        if(bills[0] != 5){
            return false;
        }else {
            five ++ ;
        }
        for (int i = 1; i < bills.length; i++) {
            if (bills[i] == 5) {
                five++;
            } else if (bills[i] == 10) {
                if (five == 0)
                    return false;
                else {
                    five--;
                    ten++;
                }
            } else if (bills[i] == 20) {
                if (five < 1) {
                    return false;
                } else if (ten >= 1 && five >= 1) {
                    five--;
                    ten--;
                } else if (five >= 3 && ten == 0) {
                    five = five - 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(20);
        int[] a = new int[100000];
        Random r = new Random();
        for (int i = 0; i < 100000; i++) {
            int i1 = r.nextInt(2);
            if (i1 == 0) {
                a[i] = 5;
            } else if (i1 == 1) {
                a[i] = 10;
            } else if (i1 == 2) {
                a[i] = 20;
            }
        }
        int[] bills = new int[]{5, 5, 5, 10, 20};
        int[] bills2 = new int[]{5, 5, 10, 10, 20};
        int[] bills3 = new int[]{10,5,5};
        StopWatch sw = new StopWatch("time : ");
        sw.start();
        boolean money = lemonadeChange(bills);
        sw.stop();
        System.out.println(sw.prettyPrint());
        System.out.println(money);
    }
}
